<!--
 * @Description: 供应商车辆维护
 * @Autor: wangwangwang
 * @Date: 2021-03-31 16:58:24
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-06-23 11:15:47
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="tabtop" style="width:380px;">
          <div v-if="ButtonList.findIndex(res=>(res.buttonValue=='HNTYSTIANJIA'))!==-1" @click="choose(1),getData()">
            <div :class="currenindex == 1 ? 'active' : 'text'">混凝土运输</div>
          </div>
          <div v-if="ButtonList.findIndex(res=>(res.buttonValue=='YWLYSTTIANJIA'))!==-1" @click="choose(2),getData()">
            <div :class="currenindex == 2 ? 'active' : 'text'">原物料运输</div>
          </div>
        </div>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">车牌号</span>
                <el-input v-model="tableParam.plateNumber" style="width: 140px;" clearable placeholder="请输入" @keyup.enter.native="initData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">状态</span>
                <el-select v-model="tableParam.enable" style="width: 140px;" placeholder="请选择">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="禁用" value="0"></el-option>
                </el-select>
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">车辆类型</span>
                <el-select v-model="tableParam.carTypeId" placeholder="请选择">
                  <el-option v-for="item in carTypelist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <el-button icon="el-icon-search" size="medium" class="bottom_all but1" @click="(tableParam.page = 1), initData()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" @click="init()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='HNTYSTIANJIA'))!==-1&&currenindex==1" size="small" class="bottom_all" style="margin-left: 0;" @click="addFromDate('添加'), stationList = []">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='YWLYSTTIANJIA'))!==-1&&currenindex==2" size="small" class="bottom_all" style="margin-left: 0;" @click="addFromDate('添加')">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='RIYONG'))!==-1" size="small" class="bottom_three" @click="Update(1)">
                <i class="icon iconfont iconicon_button_startuse" style="font-size: 12px;"></i>
                启用
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='JINYONG'))!==-1" size="small" class="bottom_four" @click="Update(0)">
                <i class="icon iconfont iconicon_button_disable1" style="font-size: 12px;"></i>
                禁用
              </el-button>
            </div>
            <div>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="load()">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div v-loading="tableLoading" class="card_info" :style="style">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection"></el-table-column>
              <template v-for="(item, index2) in table">
                <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                  <template slot-scope="scope">
                    <span v-if="scope.row.enable == 0 && item.label == '状态'" style="color:#969799;">禁用</span>
                    <span v-if="scope.row.enable == 1 && item.label == '状态'" style="color:#00A754;">启用</span>
                    <span v-if="item.label == '使用状况'">空置</span>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" size="mini" class="bottom_one" @click="updateFromDateFun(scope.row)"> <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改</el-button>
                    <a v-if="item.label == '二维码'&&scope.row.quickMarkUrl&&ButtonList.findIndex(res=>(res.buttonValue=='EWEIM'))!==-1" class="table_link" :href="'/file/' +scope.row.quickMarkUrl" download>下载二维码</a>
                    <span v-if="item.label !== '操作' && item.label !== '状态' && item.label !== '使用状况' && item.label !== '二维码'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="initData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="initData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="580px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" style="padding: 20px;" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="contractId" :rules="[{required: true,message: '请选择合同',trigger: 'change',}]">
            <span slot="label" style="color: #ff0000">合同</span>
            <el-select v-model="fromDate.contractId" placeholder="请选择" @change="changeContract">
              <el-option v-for="item in contractIdList" :key="item.id" :label="item.contractNumber+item.supplierName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-show="currenindex===1" prop="stationId" :rules="[{required: currenindex===1,message: '请选择站点',trigger: 'change',}]">
            <span slot="label" style="color: #ff0000">站点</span>
            <el-select ref="station" v-model="fromDate.stationId" placeholder="请选择" @change="$nextTick(()=>{fromDate.stationName = $refs.station.selectedLabel})">
              <el-option v-for="item in stationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="plateNumber">
            <span slot="label" style="color:  #FF0000 ;">车牌号</span>
            <el-input v-model="fromDate.plateNumber" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="carTypeId">
            <span slot="label" style="color:  #FF0000 ;">车辆类型</span>
            <el-select ref="carType" v-model="fromDate.carTypeId" placeholder="请选择" @change="$nextTick(()=>{fromDate.carTypeName = $refs.carType.selectedLabel})">
              <el-option v-for="item in carTypelist" :key="item.id" :label="item.itemName" :value="String(item.id)"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="enable">
            <el-radio-group v-model="fromDate.enable">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="车辆性质" prop="enable">
            <el-radio-group v-model="fromDate.carNature">
              <el-radio :label="1">自用</el-radio>
              <el-radio :label="2">临时</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="currenindex==1" prop="maxLoad">
            <span slot="label">最大承重</span>
            <el-input v-model="fromDate.maxLoad" type="Number" placeholder="请输入" clearable>
              <template slot="append">方</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="tare">
            <span slot="label" style="color:#FF0000;">皮重</span>
            <el-input v-model="fromDate.tare" type="Number" placeholder="请输入" clearable>
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="submitPageFromDate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  batchUpdateOrDelete,
  updateSortNum,
  saveOrUpdate,
  Download,
  initDataSource,
  editDataSource,
} from "@/api/supplier/car.js";
import { getUserInfo } from "@/utils/auth.js";
import {
  getDicType,
  queryListByUserID
} from "@/api/common.js";
import { carReg } from "@/common/RegExp.js";
export default {
  mixins: [mixin],
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (carReg(value)) {
        callback();
      } else {
        callback(new Error("请输正确的车牌号!"));
      }
    };
    return {
      UserInfo: "",
      contractIdList: [],//合同列表
      // companyIdList: [],//公司列表
      // supplierIdList: [],//供应商列表
      contractTypelist: [
        {
          id: 1,
          itemName: "混凝土运输",
        },
        {
          id: 2,
          itemName: "原物料运输",
        },
      ],
      currenindex: 1,
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      batchUpdateOrDelete: batchUpdateOrDelete,
      updateSortNum: updateSortNum,
      saveOrUpdate: saveOrUpdate,
      DownloadURL: Download,
      /*表格数据*/
      table: [],
      table1: [
        {
          label: "车牌号",
          width: "140",
          prop: "plateNumber",
          status: true,
        },
        {
          label: "车辆类型",
          width: "120",
          prop: "carTypeName",
          status: true,
        },
        {
          label: "所属单位",
          width: "260",
          prop: "company",
          status: true,
        },
        {
          label: "最大载重",
          width: "140",
          prop: "maxLoad",
          status: true,
        },
        {
          label: "皮重",
          width: "140",
          prop: "tare",
          status: true,
        },
        {
          label: "二维码",
          width: "140",
          prop: "120",
          status: true,
        },
        {
          label: "状态",
          width: "140",
          prop: "",
          status: true,
        },
        {
          label: "更新时间",
          width: "210",
          prop: "updateTime",
          status: true,
        },
        {
          label: "操作",
          width: "120",
          prop: "",
          status: true,
        },
      ],
      table2: [
        {
          label: "车牌号",
          width: "140",
          prop: "plateNumber",
          status: true,
        },
        {
          label: "车辆类型",
          width: "120",
          prop: "carTypeValue",
          status: true,
        },
        {
          label: "所属单位",
          width: "260",
          prop: "supplierName",
          status: true,
        },
        {
          label: "最大载重",
          width: "140",
          prop: "maxLoad",
          status: true,
        },
        {
          label: "皮重",
          width: "140",
          prop: "tare",
          status: true,
        },
        {
          label: "二维码",
          width: "140",
          prop: "120",
          status: true,
        },
        {
          label: "状态",
          width: "140",
          prop: "",
          status: true,
        },
        {
          label: "更新时间",
          width: "210",
          prop: "updateTime",
          status: true,
        },
        {
          label: "操作",
          width: "120",
          prop: "",
          status: true,
        },
      ],
      /*表单页类型，0为无树状，1为有树状，有树状在el-main加上display:flex*/
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        code: [
          { required: true, message: "请输入车辆自编码", trigger: "blur" },
        ],
        plateNumber: [
          { required: true, message: "请输入车牌号", trigger: "change" },
          { validator: validatePass1, trigger: "blur", }
        ],
        carTypeId: [
          { required: true, message: "请选择车辆类型", trigger: "change" },
        ],
        supplierId: [
          { required: true, message: "请选择所属单位", trigger: "blur" },
        ],
        tare: [{ required: true, message: "请输入皮重", trigger: "blur" }],
      },
      fromDate: {
        code: "",
        plateNumber: "",
        carTypeId: "",
        carTypeName: "",
        supplierName: "",
        supplierId: "",
        contractId: '',
        company: '',
        stationId: '',
        stationName: '',
        enable: 1,
        carNature: 1,
        maxLoad: "",
        tare: "",
      },
      carTypelist: [],
      stationList: [],
    };
  },
  async created() {
    this.table = this.table1;
    queryListByUserID().then((res) => {
      this.contractIdList = res.data;
    });
    this.UserInfo = await getUserInfo();
    // if (this.UserInfo.adminStatus == 1) {
    //   /**通过typeId为公司类型id查询所有公司 */
    //   getcompany({ typeId: "19da56b9e85648059e3cf1e7da497da4" }).then((res) => {
    //     this.companyIdList = res.data;
    //   });
    // } else {
    //   getUserAut().then((res) => {
    //     this.companyIdList = res.data.companyList;//公司
    //   })
    // }
    /*存储拷贝的原初始化fromDate数据*/
    this.getData();
    getDicType({ code: "vanType" }).then((res) => {
      this.carTypelist = res.data.items; //
    });
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  methods: {
    changeContract (value) {
      const newData = this.contractIdList.find(item => item.id === value);
      this.stationList = JSON.parse(newData.stations);
    },
    initData () {
      if (this.currenindex === 1) {
        initDataSource({...this.tableParam, sysType: 2}).then(res => {
          if (res.code === 1001) {
            this.tableData = res.data.data;
            this.total = res.data.totalRows;
          }
        });
      } else {
        this.gettableData();
      }
    },
    updateFromDateFun(data) {
      this.addDialogTitle = "修改"
      this.addDialog = true
      this.fromDate = { ...this.fromDate, ...data }
    },
    getData() {
      this.tableParam = {
        ...this.tableParam,
        plateNumber: "",
        enable: "",
        sysType: 2,
      };
      this.initData();
    },
    choose(num) {
      if (num === 1) {
        this.table = this.table1;
      } else {
        this.table = this.table2;
      }
      this.currenindex = num;
    },
    init() {
      /*重置数据*/
      this.tableParam.plateNumber = "";
      this.tableParam.enable = "";
      this.tableParam.carTypeId = "";
      this.initData();
    },
    submitPageFromDate() {
      this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          this.fromDate.carTypeValue = this.$fromatTable(
            this.carTypelist,
            this.fromDate.carTypeId,
            "id",
            "itemName"
          );
          let item = this.contractIdList.find((v) => v.id == this.fromDate.contractId)
          if (!item) {
            this.$message.warning('无合同信息')
            return
          }
          this.fromDate.companyId = item.exteriorCompanyId
          this.fromDate.company = item.externalCompany
          this.fromDate.supplierId = item.supplierId
          this.fromDate.supplierName = item.supplierName
          this.fromDate = {
            sysType: 2,
            ...this.fromDate,
          };
          if (this.currenindex === 1) {
            editDataSource({...this.fromDate}).then(res => {
              if (res.code === 1001) {
                this.$message.success('操作成功');
                this.addDialog = false;
                this.initData();
              }
            })
          } else {
            this.submitFromDate();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.initData();
    },
  },
};
</script>

<style lang="scss" scoped>
.dialogOtherinfo {
  .el-pagination {
    padding: 10px 0;
  }
  min-height: 530px;
  .textarea {
    width: 870px;
  }

  .el-input,
  .el-select {
    width: 375px;
  }
}
.table_link {
  margin-left: 10px;
  color: $bg_color;
}

.table_link:hover {
  opacity: 0.7;
}
.dialoginfo {
  .el-input,
  .el-select {
    width: 280px;
  }
}
</style>
